<template>
  <div class="participants card">
    <h2 class="section-title">
      参与者
      <span class="count">({{ participants.length }}/{{ maxParticipants }}人)</span>
    </h2>

    <div class="participants-list" v-if="participants.length > 0">
      <div v-for="participant in participants" :key="participant.id" class="participant-item">
        <van-image
          round
          width="40"
          height="40"
          :src="participant.avatar || '/default-avatar.png'"
        />
        <div class="info">
          <span class="name">{{ participant.nickname || participant.username }}</span>
          <span class="join-time">{{ formatJoinTime(participant.joinTime) }}</span>
        </div>
      </div>
    </div>
    <van-empty v-else description="暂无参与者" />
  </div>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'

// 配置 dayjs
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')

interface Participant {
  id: number
  username: string
  nickname?: string
  avatar?: string
  joinTime: string
}

const props = defineProps<{
  participants: Participant[]
  maxParticipants: number
}>()

const formatJoinTime = (time: string) => {
  return dayjs(time).fromNow()
}
</script>

<style scoped lang="scss">
.participants {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;

  .section-title {
    font-size: 18px;
    font-weight: bold;
    margin: 0 0 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;

    .count {
      font-size: 14px;
      color: #999;
      margin-left: 8px;
      font-weight: normal;
    }
  }

  .participants-list {
    .participant-item {
      display: flex;
      align-items: center;
      padding: 8px 0;
      border-bottom: 1px solid #f5f5f5;

      &:last-child {
        border-bottom: none;
      }

      .info {
        margin-left: 12px;
        flex: 1;
        display: flex;
        flex-direction: column;

        .name {
          font-size: 14px;
          color: #333;
          margin-bottom: 4px;
        }

        .join-time {
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}
</style>



